﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_ManufactureList.cshtml";
}
<style>
    .form .WrapTitle {
        text-align:left;padding:0 0 0 5px;line-height:14px;
    }
</style>
<script type="text/javascript">
    var status = '@ViewBag.status';
    var number = '@ViewBag.number';
    var currType = '@ViewBag.type';
    var currCompany = '';
    var currNumber = '';
    var currRowId = 0;
    var father = $.currentSubWindow();
    $(function () {
        initControl();
        gridList();
    });
    function initControl() {
        if ($('#oldEntity').val() != '') {
            var data = JSON.parse($('#oldEntity').val());
            currCompany = data.FProCompanyNO;
            currNumber = data.ProductionID;
        }
        //alert(currNumber);
        $("input[type='radio'][value=" + currType + "].category").prop("checked", true);
        $('.category').on("click", function () {
            var type = $("input[type='radio'].category:checked").val();
            if (currType != type) {
                window.location.href = '/MainData/StampingParts/SupplierListInput?status=' + status + "&number=" + number + "&type=" + type;
            }
        })
        if (status == 'I') {
            var type = $("input[type='radio'].category:checked").val();
            if (type == 0) {
                $('#FProCompanyNO').val(currCompany);
            }
            initBindSelect("#form1");
            $('#FProCompanyNO2').bindSelect();
        } else if (status == 'U') {
            $("#radio11").prop("checked", false);
            $("#radio12").prop("checked", true);
            $('#FProCompanyNO').prop("disabled", true);
            $("input[type='radio'].category").prop("disabled", true);
            initBindSelect("#form1");
            $('#FProCompanyNO2').bindSelect();
        }
    }
    function gridList() {
        if(status == 'I'){
            var type = $("input[type='radio'].category:checked").val();
            if (type == 0) {
                gridList1();
            } else {
                gridList2();
            }        
        }else if(status == 'U'){
            gridList2();
        }

    }
    function gridList1() {
        $("#grid111").show();
        $("#grid222").hide();
        $("#condition_company").hide();
        var $gridList = $("#gridList1");
        $gridList.dataGrid({
            url: "/MainData/StampingParts/GetSupplierNListJson",
            height: $(window).height() - 248,
            colModel: [
                { label: '零件编号', name: 'FProductionNO', width: 120, align: 'left', sortable: false, frozen: true },
                { label: '零件名称', name: 'FProductionName', width: 200, align: 'left', sortable: false, frozen: true },
                { label: '零件分类', name: 'FProductionTypeName', width: 80, align: 'center', sortable: false },
                { label: '备注', name: '', width: 20, align: 'left', sortable: false },
                { label: '零件ID', name: 'FProductionID', width: 80, align: 'center', hidden: true },
                { label: '零件分类编码（隐藏）', name: 'FProductionTypeNO', width: 80, align: 'center', hidden: true },
            ],
            pager: "#gridPager1",
            sortname: 'FProductionNO asc',
            viewrecords: true,
            rownumWidth: 40,
            gridComplete: function () {

            },
            loadComplete: function () {
                var rowDatas = $gridList.jqGrid("getRowData");
                var length = rowDatas.length + 1;
                if (length > 0) {
                    if (currRowId == 0) {
                        currRowId = 1;
                    } else if (currRowId > length) {
                        currRowId = length;
                    }
                }
                if (currRowId > 0) {
                    $gridList.jqGrid('setSelection', currRowId);
                    //var $grid = $('#gview_gridList2 .ui-jqgrid-bdiv');
                    //$grid.setScrollTop(length, currRowId);
                }
            },
            onSelectRow: function (rowid) {
                currRowId = rowid;
                var rowData = $gridList.jqGrid("getRowData", currRowId);
                emptyDataById("#form1");
                setDataByJson("#form1", rowData);
                $('#FProCompanyNO').val(currCompany);
                //$('#CommentExplain').val('');
                //$('#ReferenceDrawing').val('');
                initBindSelect("#form1");
                $('#btn_ok').removeClass("disabled");
            },
            onPaging: function (page_btn) {
                currRowId = 1;
            }
        });
        $gridList.jqGrid('setLabel', 0, '序号', 'rowNumberHeaderStyle');
        //$gridList.jqGrid('setFrozenColumns');
        $("#btn_search").click(function () {
            reloadGrid1();
        });
    }
    function reloadGrid1() {
        $("#gridList1").jqGrid('setGridParam', {
            postData: { Production: $('#FProductionNO2').val() },
        }).trigger('reloadGrid');
    }
    function gridList2() {
        $("#grid111").hide();
        $("#grid222").show();
        $("#condition_company").show();
        var currPage = 1;
        if (currNumber != "") {
            var data = {};
            data["sidx"] = "FProductionNO asc,FProCompanyNO asc";
            data["sord"] = "";
            data["rows"] = 50;
            data["currNumber"] = currNumber;
            $.ajaxSetup({ async: false });
            $.getJSON("/MainData/StampingParts/GetSupplierSortJson", data, function (result) {
                currPage = result.currPage;
                currRowId = result.currIndex;
            });
            $.ajaxSetup({ async: true });
        }

        var $gridList = $("#gridList2");
        $gridList.dataGrid({
            url: "/MainData/StampingParts/GetSupplierListJson",
            height: $(window).height() - 248,
            colModel: [
                { label: '零件编号', name: 'FProductionNO', width: 120, align: 'left', sortable: false, frozen: true },
                { label: '零件名称', name: 'FProductionName', width: 200, align: 'left', sortable: false, frozen: true },
                { label: '制造单位编号', name: 'FProCompanyNO', width: 120, align: 'left', sortable: false },
                { label: '制造单位名称', name: 'FProCompanyName', width: 200, align: 'left', sortable: false },
                { label: '零件分类', name: 'FProductionTypeName', width: 80, align: 'center', sortable: false },
                { label: '负责人', name: 'FResPerson', width: 100, align: 'left', sortable: false },
                { label: '参考图号', name: 'ReferenceDrawing', width: 120, align: 'left', sortable: false },
                { label: '供货描述', name: 'CommentExplain', width: 240, align: 'left', sortable: false },
                { label: '零件ID', name: 'ProductionID', width: 80, align: 'center', hidden: true },
                { label: '零件分类编码（隐藏）', name: 'FProductionTypeNO', width: 80, align: 'center', hidden: true },
            ],
            pager: "#gridPager2",
            page: currPage,
            sortname: 'FProductionNO asc,FProCompanyNO asc',
            viewrecords: true,  //是否显示行数
            rownumWidth: 40,
            gridComplete: function () {

            },
            loadComplete: function () {
                var rowDatas = $gridList.jqGrid("getRowData");
                var length = rowDatas.length + 1;
                if (length > 0) {
                    if (currRowId == 0) {
                        currRowId = 1;
                    } else if (currRowId > length) {
                        currRowId = length;
                    }
                }
                if (currRowId > 0) {
                    $gridList.jqGrid('setSelection', currRowId);
                    var $grid = $('#gview_gridList2 .ui-jqgrid-bdiv');
                    $grid.setScrollTop(length, currRowId);
                }
            },
            onSelectRow: function (rowid) {
                currRowId = rowid;
                var rowData = $gridList.jqGrid("getRowData", currRowId);
                emptyDataById("#form1");
                setDataByJson("#form1", rowData);
                //$('#FProCompanyNO').val(currCompany);
                if (status == 'I') {
                    $('#CommentExplain').val('');
                    $('#ReferenceDrawing').val('');
                }
                initBindSelect("#form1");
                $('#btn_ok').removeClass("disabled");
            },
            onPaging: function (page_btn) {
                currRowId = 1;
            }
        });
        $gridList.jqGrid('setLabel', 0, '序号', 'rowNumberHeaderStyle');
        $gridList.jqGrid('setFrozenColumns');
        $("#btn_search").click(function () {
            reloadGrid2();
        });
    }
    function reloadGrid2() {
        $("#gridList2").jqGrid('setGridParam', {
            postData: { Production: $('#FProductionNO2').val(), FProCompanyNO: $('#FProCompanyNO2').val() },
        }).trigger('reloadGrid');
    }
    function btn_ok() {
        submitForm();
    }
    function submitForm() {
        if (!$('#form1').formValid()) {
            return false;
        }
        var data = $("#form1").formSerialize();
        $.submitForm({
            url: "/MainData/StampingParts/SubmitSupplierListForm?status=" + status,
            param: data,
            close: false,
            success: function (result) {
                var type = $("input[type='radio'].category:checked").val();
                if (type == 0) {
                    reloadGrid1();
                    father.reloadGrid(result.data);
                } else {
                    reloadGrid2(result.data);
                    father.reloadGrid(result.data);
                }
            }
        })
    }
    function btn_cancel() {
        father.$.detailCloseTop();
    }
</script>
<div class="ui-layout" id="layout" style="height: 96%; width: 100%;">
    <div style="width:900px;margin:0 auto;">
        <input id="oldEntity" type="hidden" value="@ViewBag.oldEntity" />
        <form id="form1">
            <table class="form" style="width:900px;margin:5px;border-bottom:2px solid #ccc;">
                <tr>
                    <td class="formValue input-width-185" style="width:500px;padding-left:100px;text-align:left;">
                        &nbsp;
                    </td>
                    <td style="width: 400px; padding: 5px 30px 5px 0; text-align: right;">
                        <div class="btn-group" style="margin-right:20px;">
                            <a id="btn_ok" class="btn btn-primary center-block disabled" onclick="btn_ok()"><span class="glyphicon glyphicon-ok"></span> 确认</a>
                        </div>
                        <div class="btn-group">
                            <a id="btn_cancel" class="btn btn-primary center-block" onclick="btn_cancel()"><span class="glyphicon glyphicon-remove"></span> 取消</a>
                        </div>
                    </td>
                </tr>
            </table>
            <table class="form" style="width: 900px;">
                <tr>
                    <th class="formTitle" style="width:100px;">制造单位</th>
                    <td class="formValue input-width-210" style="width:220px;">
                        <select id="FProCompanyNO" name="FProCompanyNO" class="form-control required">
                            @foreach (var item in ViewBag.proCompany)
                            {
                                <option value="@item.UnitNO">@item.UnitNO @item.UnitShortName</option>
                            }
                        </select>
                    </td>
                    <th class="formTitle" style="width:80px;">供货描述</th>
                    <td class="formValue input-width-320">
                        <input id="CommentExplain" name="CommentExplain" type="text" class="form-control" value="" />
                    </td>
                </tr>
            </table>
            <table class="form" style="width: 740px;margin:5px 0 0 42px;">
                <tr>
                    <th class="formTitle WrapTitle" style="width:170px;">零件分类</th>
                    <th class="formTitle WrapTitle" style="width:120px;">零件编号</th>
                    <th class="formTitle WrapTitle" style="width:160px;">零件名称</th>
                    <th class="formTitle WrapTitle" style="width:110px;">负责人</th>
                    <th class="formTitle WrapTitle" style="width:160px;">参考图号</th>
                </tr>
                <tr>
                    <td class="formValue input-width-160">
                        <select id="FProductionTypeNO" name="FProductionTypeNO" class="form-control" disabled="disabled">
                            <option value="">&nbsp;</option>
                            @foreach (var item in ViewBag.productionType)
                            {
                                <option value="@item.FTypeNO">@item.FTypeNO @item.FTypeName</option>
                            }
                        </select>
                    </td>
                    <td class="formValue input-width-120">
                        <input id="FProductionNO" name="FProductionNO" type="text" readonly="readonly" class="form-control" value="" />
                        <input id="ProductionID" name="ProductionID" type="hidden" />
                    </td>
                    <td class="formValue input-width-160">
                        <input id="FProductionName" name="FProductionName" type="text" readonly="readonly" class="form-control" value="" />
                    </td>
                    <td class="formValue input-width-105">
                        <input id="FResPerson" name="FResPerson" type="text" class="form-control" value="" />
                    </td>
                    <td class="formValue input-width-160">
                        <input id="ReferenceDrawing" name="ReferenceDrawing" type="text" class="form-control" value="" />
                    </td>
                </tr>
            </table>
        </form>
        <div class="line-table input-width-210" style="padding:10px 0 5px 42px;">
            零件列表：
            <div class="btn-group">
                <input id="radio11" class="category" name="category" type="radio" value="0" /><label for="radio11" class="suffix">未分供应商</label> &nbsp;
                <input id="radio12" class="category" name="category" type="radio" value="1" /><label for="radio12" class="suffix">已分供应商</label> &nbsp;&nbsp;&nbsp;
            </div>
            <div id="condition_company" class="btn-group">
                <label class="prefix">制造单位</label>
                <select id="FProCompanyNO2" name="FProCompanyNO2" class="form-control">
                    <option value="">&nbsp;</option>
                    @foreach (var item in ViewBag.proCompany)
                    {
                        <option value="@item.UnitNO">@item.UnitNO @item.UnitShortName</option>
                    }
                </select>
                &nbsp;
            </div>
            <div class="btn-group">
                <label class="prefix">零件编号</label>
                <input id="FProductionNO2" name="FProductionNO2" type="text" class="form-control input-width-105" value="" />
                &nbsp;
            </div>
            <div class="btn-group">
                <a id="btn_search" class="btn btn-primary center-block btn-1" onclick=""><span class="glyphicon glyphicon-search"></span> 检索</a>
            </div>
        </div>
        
        <div style="padding: 5px 0 5px 20px;">
            <div id="grid111" style="width:100%">
                <table id="gridList1"></table>
                <div id="gridPager1"></div>
            </div>
            <div id="grid222" style="width: 100%">
                <table id="gridList2"></table>
                <div id="gridPager2"></div>
            </div>
        </div>
    </div>  
</div>

